import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:get/get.dart';
import 'package:high_help_apps/common/apis/v1/school.dart';
import 'package:high_help_apps/common/entities/misc/schools/majors_request_entity.dart';
import 'package:high_help_apps/common/entities/misc/schools/majors_response_entity.dart';
import 'package:high_help_apps/common/entities/misc/schools/schools_response_entity.dart';
import 'package:high_help_apps/common/utils/http.dart';
import 'package:high_help_apps/common/utils/toast.dart';
import 'package:high_help_apps/pages/frame/sign_up/sign_up_logic.dart';
import 'package:high_help_apps/pages/frame/sign_up/sign_up_state.dart';
import 'package:high_help_apps/pages/frame/sign_up_step/sign_up_step_logic.dart';
import 'package:high_help_apps/pages/frame/sign_up_step/sign_up_step_state.dart';

import 'auth_study_state.dart';

class AuthStudyLogic extends GetxController {
  final state = AuthStudyState();
  final SignUpStepLogic logic = Get.find<SignUpStepLogic>();
  final SignUpStepState signUpStepState = Get.find<SignUpStepLogic>().state;

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    loadAllSchools();
    loadEnterTime();
  }

  void loadAllSchools() async {

    SchoolsResponseEntity schoolsResponseEntity = await SchoolAPI.getAllSchools();

    state.schools_list = schoolsResponseEntity.detail;


  }

  Future loadSchoolMajors(int? school_id) async {

    MajorsRequestEntity majorsRequestEntity = MajorsRequestEntity().fromJson({
      "school_id": school_id
    });

    MajorsResponseEntity majorsResponseEntity = await SchoolAPI.getMajorBySchool(params: majorsRequestEntity);

    state.major_list = majorsResponseEntity.detail;

  }

  void loadEnterTime() {
    state.enter_time_list.clear();
    DateTime dateTime = DateTime.now();
    for(int i = dateTime.year; i >= dateTime.year - 45; i--)
      state.enter_time_list.add(i);

  }

  void nextPage() {

    if(
        state.school_id == null ||
        state.major_id == null ||
        state.enter_year == null) {
      toastInfo(msg: '啊哦, 学籍信息还没有填写完整呢 😓');
      return;
    }

    signUpStepState.userRegisterRequestEntity.school = state.schools_list[state.school_id ?? 0].id;
    signUpStepState.userRegisterRequestEntity.major = state.major_list[state.major_id ?? 0].id;
    signUpStepState.userRegisterRequestEntity.signTime = state.enter_time_list[state.enter_year ?? 0];

    logic.pageController.animateToPage(
        logic.currentPage + 1,
        duration: Duration(milliseconds: 300),
        curve: Curves.ease
    );
  }

  void chooseSchools(BuildContext context) {
    if(state.schools_list.isEmpty) {
      toastInfo(msg: '正在加载，请稍后……');
    } else {
      Pickers.showSinglePicker(context,
          data: state.schools_list.map((e) => e.name).toList(),
          selectData: state.schools_list[state.school_id ?? 0],
          onConfirm: (event, index) =>
              finishChooseSchools(index));
    }
  }

  void chooseMajors(BuildContext context) async {
    if(state.schools_list.isEmpty || state.school_id == null) {
      toastInfo(msg: '请先选择您的学校');
    } else {
      await loadSchoolMajors(state.school_id);
      Pickers.showSinglePicker(context,
          data: state.major_list.map((e) => e.name).toList(),
          selectData: state.major_list[state.major_id ?? 0],
          onConfirm: (event, index) =>
              finishChooseMajors(index));
    }
  }

  void chooseEnterYear(BuildContext context) {
    if(state.enter_time_list.isEmpty) {
      toastInfo(msg: '正在加载，请稍后……');
    } else {
      Pickers.showSinglePicker(context,
          data: state.enter_time_list,
          selectData: state.enter_time_list[state.enter_year ?? 0],
          onConfirm: (event, index) =>
              finishChooseEnterTime(index));
    }
  }

  void finishChooseSchools(int index) {
    state.school_id = index;
    update();
  }

  void finishChooseMajors(int index) {
    state.major_id = index;
    update();
  }

  void finishChooseEnterTime(int index) {
    state.enter_year = index;
    update();
  }


}
